package com.fita.admin.web.controller.data;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.search.data.CountrySearch;
import com.fita.admin.entity.security.Login;
import com.fita.admin.service.data.CountryService;
import com.fita.admin.web.resolver.Secure;
import com.fita.entity.authority.RoleTemplate;
import com.fita.entity.data.Country;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 国家管理
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/countries")
@MenuSetting("menu-data")
public class CountryController {
    
    @Autowired
    private CountryService countryService;

    /**
     * search page
     * @return
     */
    @RequestMapping(path = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-country-management")
    public ModelAndView search() {
    	
    	ModelAndView modelAndView = new ModelAndView("data/country/country-search");
    	
    	return modelAndView;
    }
    
    @RequestMapping(path = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-country-management")
    public ModelAndView onSearch(HttpServletRequest request, CountrySearch search, Page page) {
    	
    	ModelAndView modelAndView = new ModelAndView("data/country/country-list");
    	
    	int totals = countryService.count(Country.class, search.getPreparedFilter(false));
    	
    	if (totals > 0) {
    		
			List<Country> countries = countryService.search(Country.class, search, page.with(totals));
			
			modelAndView.addObject("countries", countries);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
		}
    	return modelAndView;
    }
    
    
    /**
     * list
     *
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET)
    @MenuSetting("submenu-country-management")
    public ModelAndView list(Page page, @Secure Login login) {
        
    	String authority = login.getAuthority();
    	
    	if (StringUtils.equals(authority, RoleTemplate.FINANCE_USER.toString())) {
			return new ModelAndView("redirect:/currencies");
		}
    	
        ModelAndView modelAndView = new ModelAndView("data/country/country-list");
        
        int totals = countryService.count(Country.class);
        if (totals > 0) {

            List<Country> countries = countryService.listByPage(Country.class, page.with(totals));
            
            modelAndView.addObject("countries", countries);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }
}
